package org.aynu.productservice.controller;

import org.aynu.productservice.common.ProductResponse;
import org.aynu.productservice.entity.ProductCategoriesEntity;
import org.aynu.productservice.entity.ProductEntity;
import org.aynu.productservice.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 产品控制器
 *
 * @author 小粥
 * @since 2025/03/03
 */
@RestController
@RequestMapping("/product")
public class ProductController {
    private final ProductService productService;
    @Autowired
    public ProductController(ProductService productService) {
        this.productService = productService;
    }

    /**
     * 选择产品
     *
     * @return {@link ProductResponse }<{@link List }<{@link ProductEntity }>>
     * @author 小粥
     * @since 2025/03/03
     *//*获取所有商品*/
    @RequestMapping(value = "/selectProduct", method = RequestMethod.POST)
    public ProductResponse<List<ProductEntity>> selectProduct(){
        return productService.selectProduct();
    }

    /**
     * 按 ID 选择产品
     *
     * @param map 地图
     * @return {@link ProductResponse }<{@link ProductEntity }>
     * @author 小粥
     * @since 2025/03/03
     *//*通过商品Id查看商品详情*/
    @RequestMapping(value = "/selectProductById", method = RequestMethod.POST)
    public ProductResponse<ProductEntity> selectProductById(@RequestBody Map<String,Object> map){
        int productId = Integer.parseInt(map.get("productId").toString());
        return productService.selectProductById(productId);
    }

    /**
     * 创建产品类别
     *
     * @param map 地图
     * @return {@link ProductResponse }<{@link Integer }>
     * @author 小粥
     * @since 2025/03/03
     *//*创建商品分类*/
    @RequestMapping(value = "/createProductCategory", method = RequestMethod.POST)
    public ProductResponse<Integer> createProductCategory(@RequestBody Map<String,Object> map){
        ProductCategoriesEntity productCategoriesEntity = new ProductCategoriesEntity();
        productCategoriesEntity.setName(map.get("name").toString());
        productCategoriesEntity.setDescription(map.get("description").toString());
        return productService.createProductCategory(productCategoriesEntity);
    }

    /*通过商品分类显示对应的商品*/
    @RequestMapping(value = "/selectProductByCategoryId", method = RequestMethod.POST)
    public ProductResponse<List<ProductEntity>> selectProductByCategoryId(@RequestBody Map<String,Object> map){
        int categoryId = Integer.parseInt(map.get("categoryId").toString());
        return productService.selectProductByCategoryId(categoryId);
    }

    /**
     * 更新产品类别
     *
     * @param map 地图
     * @return {@link ProductResponse }<{@link Integer }>
     * @author 小粥
     * @since 2025/03/03
     *//*更新商品分类*/
    @RequestMapping(value = "/updateProductCategory", method = RequestMethod.POST)
    public ProductResponse<Integer> updateProductCategory(@RequestBody Map<String,Object> map){
        ProductCategoriesEntity productCategoriesEntity = new ProductCategoriesEntity();
        productCategoriesEntity.setCategoryId(Integer.parseInt(map.get("categoryId").toString()));
        productCategoriesEntity.setName(map.get("name").toString());
        productCategoriesEntity.setDescription(map.get("description").toString());
        return productService.updateProductCategory(productCategoriesEntity);
    }

    /**
     * 删除产品类别
     *
     * @param map 地图
     * @return {@link ProductResponse }<{@link Integer }>
     * @author 小粥
     * @since 2025/03/03
     *//*逻辑删除商品分类*/
    @RequestMapping(value = "/deleteProductCategory", method = RequestMethod.POST)
    public ProductResponse<Integer> deleteProductCategory(@RequestBody Map<String,Object> map){
        int categoryId = Integer.parseInt(map.get("categoryId").toString());
        return productService.deleteProductCategory(categoryId);
    }

    /**
     * 选择产品类别
     *
     * @return {@link ProductResponse }<{@link List }<{@link ProductCategoriesEntity }>>
     * @author 小粥
     * @since 2025/03/03
     *//*获取商品分类表*/
    @RequestMapping(value = "/selectProductCategories", method = RequestMethod.POST)
    public ProductResponse<List<ProductCategoriesEntity>> selectProductCategories(){
        return productService.selectProductCategories();
    }

    /**
     * 创建产品
     *
     * @param map 地图
     * @return {@link ProductResponse }<{@link Integer }>
     * @author 小粥
     * @since 2025/03/03
     *//*创建商品*/
    @RequestMapping(value = "/createProduct", method = RequestMethod.POST)
    public ProductResponse<Integer> createProduct(@RequestBody Map<String,Object> map){
        ProductEntity productEntity = new ProductEntity();
        productEntity.setName(map.get("name").toString());
        productEntity.setDescription(map.get("description").toString());
        productEntity.setPrice(new BigDecimal(map.get("price").toString()));
        productEntity.setStockQuantity(Integer.parseInt(map.get("stockQuantity").toString()));
        productEntity.setImageUrl(map.get("imageUrl").toString());
        int categoryId = Integer.parseInt(map.get("categoryId").toString());
        return productService.createProduct(productEntity,categoryId);
    }

    /**
     * 删除产品
     *
     * @param map 地图
     * @return {@link ProductResponse }<{@link Integer }>
     * @author 小粥
     * @since 2025/03/03
     *//*逻辑删除商品*/
    @RequestMapping(value = "/deleteProduct", method = RequestMethod.POST)
    public ProductResponse<Integer> deleteProduct(@RequestBody Map<String,Object> map){
        int productId = Integer.parseInt(map.get("productId").toString());
        return productService.deleteProduct(productId);
    }

    /**
     * 更新产品
     *
     * @param map 地图
     * @return {@link ProductResponse }<{@link Integer }>
     * @author 小粥
     * @since 2025/03/03
     *//*更新商品信息*/
    @RequestMapping(value = "/updateProduct", method = RequestMethod.POST)
    public ProductResponse<Integer> updateProduct(@RequestBody Map<String,Object> map){
        ProductEntity productEntity = new ProductEntity();
        productEntity.setProductId(Integer.parseInt(map.get("productId").toString()));
        productEntity.setName(map.get("name").toString());
        productEntity.setDescription(map.get("description").toString());
        productEntity.setPrice(new BigDecimal(map.get("price").toString()));
        productEntity.setStockQuantity(Integer.parseInt(map.get("stockQuantity").toString()));
        productEntity.setImageUrl(map.get("imageUrl").toString());
        return productService.updateProduct(productEntity);
    }

    /**
     * 获取产品库存
     *
     * @param map 地图
     * @return {@link ProductResponse }<{@link Integer }>
     * @author 小粥
     * @since 2025/03/03
     *//*获取商品库存*/
    @RequestMapping(value = "/getStock", method = RequestMethod.POST)
    public ProductResponse<Integer> getProductStock(@RequestBody Map<String,Object> map){
        int productId = Integer.parseInt(map.get("productId").toString());
        return productService.getProductStock(productId);
    }

    /**
     * 更新产品库存
     *
     * @param map 地图
     * @return {@link ProductResponse }<{@link Integer }>
     * @author 小粥
     * @since 2025/03/03
     *//*更新商品库存*/
    @RequestMapping(value = "/updateStock", method = RequestMethod.POST)
    public ProductResponse<Integer> updateProductStock(@RequestBody Map<String,Object> map){
        int productId = Integer.parseInt(map.get("productId").toString());
        int quantity = Integer.parseInt(map.get("quantity").toString());
        return productService.updateProductStock(productId,quantity);
    }
}